<?php
require_once('dedefunctions.php');	
require_once('dede.string.helper.php');
require_once('dede.util.helper.php');	
require_once('db.php');

$title = '测试文章';
$body = '测试文章内容';
$typeid = '63433';

$flag = '';
$notpost = 0;
$typeid2 = '';
$autokey = 0;
$remote = 0;
$dellink = 0;
$autolitpic = 0;
$click = 0;
$channelid = '1';


//对保存的内容进行处理
$writer='admin';
$source='未知';

$pubdate = GetMkTime(date("Y-m-d"));
$senddate = time();
$sortrank = AddDay($pubdate,$sortup);
$ismake = -1;
$title = preg_replace("#\"#", '＂', $title);
$title = htmlspecialchars(cn_substrR($title,120));
$shorttitle = cn_substrR($shorttitle,36);
$color =  cn_substrR($color,7);
$writer =  cn_substrR($writer,20);
$source = cn_substrR($source,30);
$description = cn_substrR($description,240);
$keywords = cn_substrR($keywords,60);
$filename = trim(cn_substrR($filename,40));
$userip = GetIP();
$isremote  = 0;
$serviterm="";


$adminid = 'crazyant_ps2';


function GetIndexKey($arcrank, $typeid, $sortrank=0, $channelid=1, $senddate=0, $mid=1)
{
        global $db,$senddate,$typeid2;
        if(empty($typeid2)) $typeid2 = 0;
        if(empty($senddate)) $senddate = time();
        if(empty($sortrank)) $sortrank = $senddate;
        $iquery = "
          INSERT INTO `dede_arctiny` (`arcrank`,`typeid`,`typeid2`,`channel`,`senddate`, `sortrank`, `mid`)
          VALUES ('$arcrank','$typeid','$typeid2' , '$channelid','$senddate', '$sortrank', '$mid') ";
        $db->query($iquery);
        $aid = $db->getLastId();
        return $aid;
}

$body = addslashes($body);


//生成文档ID
$arcID = GetIndexKey($arcrank,$typeid,$sortrank,$channelid,$senddate,$adminid);

if(empty($arcID))
{
	echo "无法获得主键，因此无法进行后续操作！";
	exit();
}
if(trim($title) == '')
{
	echo '标题不能为空';
	exit();
}



//保存到主表
$query = "INSERT INTO `dede_archives`(id,typeid,typeid2,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,
color,writer,source,litpic,pubdate,senddate,mid,voteid,notpost,description,keywords,filename,dutyadmin,weight)
VALUES ('$arcID','$typeid','$typeid2','$sortrank','$flag','$ismake','$channelid','$arcrank','$click','$money',
'$title','$shorttitle','$color','$writer','$source','$litpic','$pubdate','$senddate',
'$adminid','$voteid','$notpost','$description','$keywords','$filename','$adminid','$weight');";

if(!$db->query($query))
{
	$db->query("DELETE FROM `dede_arctiny` WHERE id='$arcID'");
	exit();
}

$useip = GetIP();
$query = "INSERT INTO `dede_addonarticle`(aid,typeid,redirecturl,templet,userip,body) Values('$arcID','$typeid','','','$useip','$body')";
$db->query($query);


?>